package com.tjp.subject.domain.handler.subject;

import com.tjp.subject.common.enums.SubjectInfoTypeEnum;
import com.tjp.subject.domain.convert.SubjectRadioConverter;
import com.tjp.subject.domain.entity.SubjectAnswerBO;
import com.tjp.subject.domain.entity.SubjectInfoBO;
import com.tjp.subject.domain.entity.SubjectOptionBO;
import com.tjp.subject.infra.basic.entity.SubjectRadio;
import com.tjp.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: jz-club
 * @description: 单选抽象接口策略类
 * @author: aaronTang
 * @create: 2024-02-29
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //实际往表中插入单选题的逻辑
        List<SubjectRadio> subjectRadioList = new LinkedList<>();
        //subjectInfoBO.getOptionList()会存在空指针问题
        subjectInfoBO.getOptionList().forEach(option -> {
            SubjectRadio subjectRadio = SubjectRadioConverter.INSTANCE.convertBOToRadioEntity(option);
            //填充题目id
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadioList.add(subjectRadio);
        });

        subjectRadioService.batchInsert(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(Long subjectId) {
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryBySubjectId(subjectId);

        List<SubjectAnswerBO> answerBOList = new ArrayList<>();
        subjectRadioList.forEach(subjectRadio -> {
            SubjectAnswerBO subjectAnswerBO = SubjectRadioConverter.INSTANCE.convertSubjectRadioToAnswerEntity(subjectRadio);
            answerBOList.add(subjectAnswerBO);
        });

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(answerBOList);

        return subjectOptionBO;
    }
}
